//
//  CookBookTableController.m
//  FoodCorner
//
//  Created by Summer on 16/3/1.
//  Copyright © 2016年 Summer. All rights reserved.
//

#import "CookBookTableController.h"
#import "CategroyListViewController.h"
#import "ImageAndTextTableViewCell.h"
#import "SearchViewController.h"
#import "ListModel.h"
#import "TableView.h"
NSUInteger leftIndex=0;//左侧选中索引
@interface CookBookTableController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSMutableDictionary *categoryDictionary;
@property (nonatomic,strong) NSMutableArray *categorArray;
@property (nonatomic,strong) UISwipeGestureRecognizer *leftSwipe;
@property (nonatomic,strong) TableView *tableView;


@end
static NSString *cellIndetifier=@"ImageAndTextTableViewCell";
@implementation CookBookTableController

-(void)loadView
{
    _tableView = [[TableView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view  = _tableView;
}
///懒加载
-(NSMutableDictionary *)categoryDictionary
{
    if (_categoryDictionary == nil) {
        _categoryDictionary = [NSMutableDictionary dictionary];
    }
    return _categoryDictionary;
}
///懒加载
-(NSMutableArray *)categorArray
{
    if (_categorArray == nil) {
        _categorArray = [NSMutableArray array];
    }
    return _categorArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [NSThread sleepForTimeInterval:1];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self.tableView.tableView registerNib:[UINib nibWithNibName:cellIndetifier bundle:nil] forCellReuseIdentifier:@"ImageAndTextTableViewCell"];
    self.tableView.tableView.delegate = self;
    self.tableView.tableView.dataSource = self;
    self.tableView.rightTableView.delegate  = self;
    self.tableView.rightTableView.dataSource = self;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemStop) target:self action:@selector(clickBackButtonAciotn)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemSearch) target:self action:@selector(searchControllerAction)];
    
    
    self.leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(clickBackButtonAciotn)];
    _leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:_leftSwipe];
    //[self viewBlurEffect];
    [self reloadData];
}
- (void)clickBackButtonAciotn
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)searchControllerAction
{
    SearchViewController *search = [[SearchViewController alloc]init];
    [self.navigationController pushViewController:search animated:YES];
}


#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incombplete implementation, return the number of rows
    ///获取现在所在数组array中所包含的数据数组
    if (tableView.tag == 101) {
     return  self.categorArray.count;
    }else
    {
        NSArray *array = [self.categoryDictionary objectForKey:self.categorArray[leftIndex]];
        return  [array count];
    }

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    if (tableView.tag == 101) {
        return 80;
    }
    else
    {
        return 40;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (tableView.tag == 101) {
        ImageAndTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
        cell.text.text = self.categorArray[indexPath.row];
        return cell;
    }else
    {
        UITableViewCell *cell=[[UITableViewCell alloc]init];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        
        
        NSArray *array = [self.categoryDictionary objectForKey:self.categorArray[leftIndex]];
        ListModel *model = array[indexPath.row];

        cell.textLabel.text = model.name;
        return cell;

    }
    
    
    // Configure the cell...
   }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView.tag == 101) {
        leftIndex = indexPath.row;
        [self.tableView.rightTableView reloadData];
        [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:(UITableViewScrollPositionMiddle) animated:YES];
    }else{
    ///获取这个数组中所有对象，通过indexPath.row来获取对应每个Model对象
    NSArray *array = [self.categoryDictionary objectForKey:self.categorArray[indexPath.section]];
    ListModel *model = array[indexPath.row];
    ///获取准备跳转的下个页面的对象
    CategroyListViewController *listVC = [[CategroyListViewController alloc]init];
    ///传到下一个页面一个字符串其中字符串为下个页面进行解析的主要字符串（例如：菜肴）
    listVC.categroy = model.name;
    
    [self.navigationController pushViewController:listVC animated:YES];

    }
}

#pragma mark - 获取数据列表。其中model
- (void)reloadData
{
    //通过解析获取plsit文件中的数据
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Food" ofType:@"plist"];
    //数据中最外层为数组由数组接受
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    ///通过遍历数组获取单个小字典
    for (NSDictionary *dict in array) {
        ///获取关键字为list字符串的数组
        NSArray *array = dict[@"list"];
        ///以关键字name 为将要存储数据时候的关键字。
        NSString *groupKey = dict[@"name"];
        ///创建数组通过此数组来添加遍历出来的Model
        NSMutableArray *modelArray = [NSMutableArray array];
        for (NSDictionary *dicts in array) {
            //NSLog(@"%@",dicts);
            ListModel *model = [[ListModel alloc] init];
            [model setValuesForKeysWithDictionary:dicts];
            [modelArray addObject:model];
            ///将已经添加到数组modelArray的数据以关键字groupKey来添加到字典中
            [self.categoryDictionary setObject:modelArray forKey:groupKey];
        }
    }
    ///将字典中的所有关键字添加到数组中。
    [self.categorArray addObjectsFromArray:self.categoryDictionary.allKeys];
    
}
#pragma mark ----添加视图模糊效果，增加视觉效果
- (void)viewBlurEffect
{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    imageView.image = [UIImage imageNamed:@"placeholder"];
    //毛玻璃效果
    UIVisualEffectView *visualView =[[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:(UIBlurEffectStyleLight)]];
    visualView.alpha = 0.9;
    visualView.frame=[UIScreen mainScreen].bounds;
    
    [imageView addSubview:visualView];
    
    self.tableView.tableView.backgroundView = imageView;
    
    self.tableView.tableView.separatorEffect = [UIBlurEffect effectWithStyle:(UIBlurEffectStyleDark)];
}
@end
